<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/4/22
 * Time: 17:05
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Problem extends Base
{
    /**
     * 常见问题列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $problem_title = trim(input('problem_title'), ' ');
            $start_time = input('start_time');
            $end_time = input('end_time');
            
            $where = [];
            if(isset($problem_title) && $problem_title != ''){
                $where['problem_title'] = ['like', '%' . $problem_title . '%'];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['problem_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['problem_time'] = ['>', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['problem_time'] = ['<', $end_time];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('problem')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('problem')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('problem_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 问题详情
     * @return mixed
     */
    public function detail(){
        $problem_id = input('problem_id');
        $problem = Db::table('up_problem')
            ->field('problem_title, problem_time,  problem_content')
            ->where('problem_id', $problem_id)
            ->find();
        return $this->fetch('problem/detail', ['problem' => $problem]);
    }
    
    /**
     * 添加常见问题
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $problem_title = trim(input('problem_title'), ' ');
            $problem_content = input('problem_content');
            $insert = [
                'problem_title' => $problem_title,
                'problem_content' => $problem_content,
                'problem_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_problem')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    
    /**
     * 编辑常见问题
     * @return mixed|\think\response\Json
     */
    public function update(){
        $problem_id = input('problem_id');
        if(Request::instance()->isPost()){
            $problem_title = trim(input('problem_title'), ' ');
            $problem_content = input('problem_content');
            $update = [
                'problem_title' => $problem_title,
                'problem_content' => $problem_content,
            ];
            $res = Db::table('up_problem')->where('problem_id', $problem_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }

        $problem = Db::table('up_problem')
            ->field('problem_id, problem_title, problem_content')
            ->where('problem_id', $problem_id)
            ->find();
        $this->assign([
            'problem' => $problem,
            'problem_id' => $problem_id
        ]);
        return $this->fetch();
    }
    
    /**
     * 删除常见问题
     * @return \think\response\Json
     */
    public function del(){
        $problem_id = input('id');
        $res = Db::table('up_problem')->delete($problem_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}